package com.topwalk.step01.thread_local;

import lombok.extern.slf4j.Slf4j;

/**
 * 光闸
 * 
 * @author tangfl
 */
@Slf4j
public class ThreadLocalDemo {
    public static final ThreadLocal<String> THREAD_LOCAL = ThreadLocal.withInitial(() -> {
        log.info("invoke initial value");
        return "default value";
    });

    public static void main(String[] args) throws InterruptedException {
        
        new Thread(() ->{
            THREAD_LOCAL.set("first thread");
            log.info(THREAD_LOCAL.get());
        }).start();

        new Thread(() ->{
            THREAD_LOCAL.set("second thread");
            log.info(THREAD_LOCAL.get());
        }).start();

        new Thread(() ->{
            THREAD_LOCAL.set("third thread");
            THREAD_LOCAL.remove();
            log.info(THREAD_LOCAL.get());
        }).start();

        new Thread(() -> log.info(THREAD_LOCAL.get())).start();

        Thread.sleep(1L);
        
    }
}
